#include <iostream>
#include <string>
#include <curl/curl.h>
#include <tinyxml.h>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include "FlickR_Img.h"
#define BASE_URL "static.flickr.com/"

FlickR_Img::FlickR_Img(int farm, std::string id, bool isFamily, bool isFriend,
		     bool isPublic,
		     std::string owner, std::string secret, int server, 
		     std::string title):
  _farm(farm),
  _id(id),
  _isFamily(isFamily),
  _isFriend(isFriend),
  _isPublic(isPublic),
  _owner(owner),
  _secret(secret),
  _server(server),
  _title(title){
  
  }

FILE* temp_i;
size_t write_data_(void * ptr, size_t size, size_t nmeb, void *stream){
  return fwrite(ptr,size,nmeb,temp_i);
}

FILE* FlickR_Img::getSmallPicture(){
  std::ostringstream filename;
  filename << _id << "_" << _secret << "_s.jpg";
  
  temp_i = fopen(filename.str().c_str(),"w+");
  
  std::string url = getURL();

  {
    CURL* curl;
    curl = curl_easy_init();
    curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data_);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }

  return temp_i;
}

std::string FlickR_Img::getURL(){
  std::ostringstream url;
  url << "http://farm" << _farm << "." << BASE_URL << _server << "/" <<  _id << "_" << _secret << "_s.jpg";
  return url.str();
}

std::string FlickR_Img::getURL_Large(){
  std::ostringstream url;
  url << "http://farm" << _farm << "." << BASE_URL << _server << "/" <<  _id << "_" << _secret << ".jpg";
  return url.str();
}

std::string FlickR_Img::getSmallPicture_str(){
  std::ostringstream filename;
  filename << "tmp/" <<  _id << "_" << _secret << "_s.jpg";
  
  // download the file if it doesn't exists
  if(!boost::filesystem::exists(filename.str())){
    temp_i = fopen(filename.str().c_str(),"w+");
    
    std::string url = getURL();
    
    {
      CURL* curl;
      curl = curl_easy_init();
      curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
      curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data_);
      curl_easy_perform(curl);
      curl_easy_cleanup(curl);
    }
    fclose(temp_i);
  }
  
  return filename.str();
}

std::string FlickR_Img::getLargePicture_str(){
  std::ostringstream filename;
  filename << "tmp/" <<  _id << "_" << _secret << ".jpg";
  
  // download the file if it doesn't exists
  if(!boost::filesystem::exists(filename.str())){
    temp_i = fopen(filename.str().c_str(),"w+");
    
    std::string url = getURL_Large();
    
    {
      CURL* curl;
      curl = curl_easy_init();
      curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
      curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data_);
      curl_easy_perform(curl);
      curl_easy_cleanup(curl);
    }
    fclose(temp_i);
  }
  
  return filename.str();
}


int FlickR_Img::getFarm(){
  return _farm;
}

std::string FlickR_Img::getId(){
  return _id;
}

bool FlickR_Img::isFamily(){
  return _isFamily;
}

bool FlickR_Img::isFriend(){
  return _isFriend;
}

bool FlickR_Img::isPublic(){
  return _isPublic;
}

std::string FlickR_Img::getOwner(){
  return _owner;
}

std::string FlickR_Img::getSecret(){
  return _secret;
}

int FlickR_Img::getServer(){
  return _server;
}

std::string FlickR_Img::getTitle(){
  return _title;
}

FlickR_Img::~FlickR_Img(){}
